package com.caishi.lkx.user.api;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.context.RequireContext;
import com.caishi.lkx.common.role.Role;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.user.ienum.status.UserStatus;
import com.caishi.lkx.user.ienum.type.UserAccountType;
import com.caishi.lkx.user.ienum.type.UserType;
import com.caishi.lkx.user.model.*;
import com.caishi.lkx.user.service.*;
import com.zzw.common.exception.BizException;
import com.zzw.common.utils.CommonUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "渠道商管理员相关")
@RestController
@RequestMapping("/user/cu/channel/business/user")
@Roles({Role.admin})
public class ChannelBusinessApi extends AdminBaseApi<IChannelBusinessService, ChannelBusinessModel, String> {
    @Resource
    private IChannelBusinessService channelBusinessService;

    @Resource
    private ILoginInfoService loginInfoService;

    @Resource
    private IActionRoleService actionRoleService;

    @Resource
    private IPositionService positionService;

    @Resource
    private IUserService<UserModel> userService;


   @Override
   @GetMapping("/page")
   @RequireContext(value = com.caishi.lkx.common.context.Role.CHANNEL)
   public IPage<ChannelBusinessModel> page(@ModelAttribute Page<ChannelBusinessModel> page, @ModelAttribute ChannelBusinessModel model, @NotNull JSONObject otherParam){
       model.setGroupId(context.getGroupId());
       Map<String, Object> query = clearReturnEmptyStr(model.toMap());
       IPage<ChannelBusinessModel> result = channelBusinessService.search(clearReturnEmptyStr(query), page, page.isSearchCount());
       return result.setRecords(result.getRecords().parallelStream().map(d -> {
           d.setMobile(CommonUtil.mobileSafe(d.getMobile()));
           var List =  actionRoleService.selectUserRoles(d.getId());
           d.setRoleNames(List.stream().map(ActionRoleModel::getName).collect(Collectors.toList()));
           d.setLoginData(loginInfoService.userLastLoginData(d.getId()));
           return d;
       }).collect(Collectors.toList()));
   }

    @PostMapping("/enable/{status}/{id}")
    public void settingEnable(@PathVariable String id, @PathVariable Boolean status) {
        channelBusinessService.lockUser(status, id);
    }

    @GetMapping("/edit/detail/{id}")
    public ChannelBusinessModel adminEditDetail(@PathVariable String id) {
        ChannelBusinessModel model = channelBusinessService.getUserDetail(id);
        model.setRoles(actionRoleService.selectUserRoles(model.getId()));

        return model;
    }


    @PostMapping("/edit/add")
    @RequireContext(value = com.caishi.lkx.common.context.Role.CHANNEL)
    public ChannelBusinessModel adminUserAdd(
            @ModelAttribute ChannelBusinessModel model
    ) throws BizException {
        model.setUserType(UserType.channelBusiness.getValue());
        model.setUserStatus(UserStatus.normal);
        model.setGroupId(context.getGroupId());
        return channelBusinessService.addUser(model, UserAccountType.username);
    }

    @PutMapping("/edit/update")
    @Transactional(rollbackFor = Exception.class)
    public ChannelBusinessModel adminUserUpdate(
            @ModelAttribute ChannelBusinessModel model
    ) {
        model.setMobile(null);
        channelBusinessService.update(model);
        actionRoleService.userSettingRolesByIds(model.getId(), model.getRoleIds());
        return model;
    }

    @Override
    public IChannelBusinessService loadService() {
        return channelBusinessService;
    }

    @DeleteMapping("/delete")
    @Transactional
    public Boolean delete(@RequestParam String id) {
        return channelBusinessService.delete(id);
    }

}
